package com.kele.es_project.controller;

import com.alibaba.fastjson.JSON;
import com.kele.es_project.api.R;
import com.kele.es_project.pojo.Content;
import com.kele.es_project.service.ContentService;
import com.kele.es_project.utils.HtmlParseUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.directory.SearchResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import java.util.concurrent.TimeUnit;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2021/4/6
 * @Content:
 * @Description：
 */
@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private  RestHighLevelClient restHighLevelClient;

    @GetMapping("/pare/{keyword}")
    @ResponseBody
    public boolean pareContent(@PathVariable("keyword") String keyword) throws Exception{
        boolean b = contentService.pareContent(keyword);
        return b;
    }

    //搜索
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public  List<Map<String,Object>> search(@PathVariable("keyword")  String keyword,@PathVariable("pageNo")int pageNo,@PathVariable("pageSize") int pageSize) throws Exception{
        if (pageNo==0){
            pageNo=1;
        }
        List<Map<String, Object>> list = contentService.search(keyword, pageNo, pageSize);
        return list;
    }
    @GetMapping("/search/suggest")
    @ResponseBody
    public R<List<String>> searchSuggest(@RequestParam(required = false, defaultValue = "") String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return R.success(new ArrayList<>(Arrays.asList("Welcome", "Hello")));
        }
        return R.success(new ArrayList<>(Arrays.asList(keyword, keyword + "怎么样")));
    }

}
